<route lang="json5">
	{
	needLogin: true,
	style: {
	navigationStyle: 'default',
	navigationBarTitleText: '消息通知设置',
	},
	}
</route>

<script lang="ts" setup>
	import {
		setNotificationChannel,
		getNotificationChannelStatus,
		setNotificationChannelStatus,
	} from '@/service/common/setNotificationUtils'
	import { noticeConfigApi, noticeConfigDetailApi } from '@/service/my'
	import { useMessage } from 'wot-design-uni'
	const receiveMessage = ref(2)
	const receiveNotice = ref(2)
	const receiveGroup = ref(2)
	const receiveIm = ref(2)

	const message = useMessage()

	onUnload(async () => {
		console.info('------------------------>onUnload')
		noticeConfig()
	})

	// 页面启动
	onLoad(async () => {
		console.info('------------------------>onLoad')
		await getNoticeConfigDetail()
	})

	// 获取配置详情
	async function getNoticeConfigDetail() {
		const res : any = await noticeConfigDetailApi()
		console.log('获取配置详情:', res)
		if (res?.code === 200) {
			receiveMessage.value = res?.data?.receiveMessage
			receiveNotice.value = res?.data?.receiveNotice
			receiveGroup.value = res?.data?.receiveGroup
			receiveIm.value = res?.data?.receiveIm
		} else {
			console.error('获取配置详情失败:', res?.msg)
		}
	}

	// 更新配置
	async function noticeConfig() {
		const res : any = await noticeConfigApi(
			receiveMessage.value,
			receiveNotice.value,
			receiveGroup.value,
			receiveIm.value,
		)
		if (res?.code === 200) {
			// 消息推送管理
			const channelList = [
				{
					channelId: 'group', // 讨论组通知
					channelName: '讨论组',
					// importance: plus.android.invoke('android.app.NotificationManager', 'IMPORTANCE_DEFAULT'),
					description: '讨论组通知',
					enabled: receiveGroup.value === 1,
				},
				{
					channelId: 'circle', // 讨论圈通知
					channelName: '讨论圈',
					// importance: plus.android.invoke('android.app.NotificationManager', 'IMPORTANCE_DEFAULT'),
					description: '讨论圈通知',
					enabled: receiveIm.value === 1,
				},
			]
			// 设置通知渠道
			setNotificationChannel(channelList)
			uni.showToast({
				title: '已更新配置',
				icon: 'success',
			})
		} else {
			console.error('更新配置失败！')
		}
	}

	const beforeChange = ({ value, resolve }) => {
		message
			.confirm(value === 1 ? '是否打开消息通知' : '是否关闭消息通知')
			.then(() => {
				resolve(true)
			})
			.catch(() => {
				resolve(false)
			})
	}

	function handleMsgNotices({ value }) {
		// 消息通知设置
		console.log(receiveMessage.value)
		if (receiveMessage.value === 2) {
			receiveNotice.value = 2
			receiveGroup.value = 2
			receiveIm.value = 2
		}
		// noticeConfig()
	}

	function handleNotices({ value }) {
		// 公告
		console.log(receiveNotice.value)
		// noticeConfig()
	}

	function handleGroupValue({ value }) {
		// 讨论组
		console.log(receiveGroup.value)
		// noticeConfig()
	}

	function handleCircleValue({ value }) {
		// 讨论圈
		console.log(receiveIm.value)
		// noticeConfig()
	}
</script>

<template>
	<view class="main_container">
		<view class="top_container">
			<wd-text text="消息通知" size="20rpx" line-height="80rpx" style="margin-left: 20rpx" />

			<wd-cell center custom-class="wd-cell-font" size="large">

				<template #title>
					<view class="cell-title">讨论组</view>
				</template>
				<view class="custom-value">
					<wd-switch v-model="receiveGroup" v-on:change="handleGroupValue" size="20px" :active-value="1"
						:inactive-value="2" :disabled="receiveMessage === 2" />
				</view>
			</wd-cell>

			<wd-cell center style="margin-top: 15rpx" custom-class="wd-cell-font" size="large">
				<template #title>
					<view class="cell-title">讨论圈</view>
				</template>
				<view class="custom-value">
					<wd-switch v-model="receiveIm" v-on:change="handleCircleValue" size="20px" :active-value="1"
						:inactive-value="2" :disabled="receiveMessage === 2" />
				</view>
			</wd-cell>
		</view>
	</view>
</template>

<style scoped>
	.main_container {
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.top_container {
		margin: 20rpx;
	}

	.cell-title {
		font-size: 28rpx;
	}

	.wd-cell-font {
		font-size: 10rpx;
		font-weight: bold;
		border-radius: 5rpx;
	}

	:deep(.custom-value) {
		position: absolute;
		right: 0;
		height: 20px;
		white-space: nowrap;

		transform: translate(0, -50%);
	}
</style>